<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="truckModel" :rules="formtruck"
      ref="TruckForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="车辆类型" prop="truckTypeId" label-width="120px">
        <el-col :span="16">
           <el-select v-model="truckModel.truckTypeId">
              <el-option
                    v-for="item in parentTruckType"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
            </el-option>
            </el-select>
        </el-col>
      </el-form-item>
       <el-form-item label="车牌号码" prop="licensePlate" label-width="120px">
        <el-col :span="16">
         <el-input :maxlength="32" v-model="truckModel.licensePlate" placeholder="请输入车牌号码"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item label="所属车队" prop="fleetId" label-width="120px">
        <el-col :span="16">
           <el-select v-model="truckModel.fleetId">
              <el-option
                    v-for="item in parentFleet"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
            </el-option>
            </el-select>
          </el-col>
      </el-form-item>
     <el-form-item label="GPS设备ID" prop="deviceGpsId" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckModel.deviceGpsId" placeholder="请输入GPS设备ID"></el-input></el-col>
      </el-form-item>
       <el-form-item label="准载重量" prop="allowableLoad" label-width="120px">
        <el-col :span="16">
         <el-input :maxlength="32" v-model="truckModel.allowableLoad" placeholder="请输入准载重量(千克)"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item label="准载体积" prop="allowableVolume" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="truckModel.allowableVolume" placeholder="请输入准载体积(方)"></el-input></el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('TruckForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import carApi from '@/api/car.js'
export default {
   name: "truck-edit",
   props: ['parentTruckType','parentFleet'],
    data() {
    return {
      loading: false,
      truckModel: {
        truckTypeId:"",
        licensePlate: '',
        fleetId:'',
        deviceGpsId: '',
        allowableLoad:'',
        allowableVolume:'',
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formtruck: {
        truckTypeId: [
          { required: true, message: '请输入车辆类型', trigger: 'blur' },
        ],
        licensePlate:[
          {required:true,message:'请输入车牌号码',trigger:'blur'}
        ],
        fleetId:[
          {required:true,message:'请输入车队名称',trigger:'blur'}
        ]
      }
    }
 },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveTruck()
          }
        } else {
          return false
        }
      })
    },
    doSaveTruck() {
      this.loading = true
      console.log(this.truckModel)
      carApi.save(this.truckModel)
      .then(res => {
        console.log(res)
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(err => {
        console.error(err);
      })

    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['TruckForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
   mounted: function() {
    this.$nextTick(() => {
      this.$on('openAddTruckDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增车辆'
        this.dialogProps.visible = true
      })
    })
  }
}
</script>

<style>

</style>
